<?php
/**
 * User Social Network Connection
 * 
 * It will display the list of social network that the user is connected with
 * 
 * @author Xavier VILAIN
 * @package Hive\User
 * @license #license-hive-social#
 */

use Fox\Html\Screen;
use Hive\Ressource\Source\Parser\Linkedin;
use Fox\Html\Button;
use Fox\Html\Input;
use Fox\Generator;
use Fox\Html\Header\Content;
$xview = Xview::instance();
$id = $xview->application()->getId();
// Customizing
$customizing = Hive\User\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'users','act'=>'authentication','sac' => null));

// Capture LinkedIn Connection Code
$user = User::current();
$last_update = Msg(Hive::BUNDLE,Hive::LAST_CONNECTION_ON,
					Fox\Utilities\DateTimes::toFullString($user->lastConnectionOn()));
$last_update = '<br><span class="i sub-title-mini"> '.$last_update.'</span>';

// Page Title
$header = new Content('page-header',Msg(Hive::BUNDLE,Hive::MENU_USER_AUTHENTICATION).$last_update);
$html = null;

$data = User::current()->getData();

$screen = new Screen('sUserInfo','isave',null,true);
$screen->action(Generator::url('users','preferences'));

$screen->buttonBarClass('text-center');
$screen->block()->add(new Input('opass',true))
		->inputType('password');
$screen->block()->add(new Input('npass',true))
		->inputType('password');
$screen->block()->add(new Input('cpass',true))
		->inputType('password');
$screen->initialize($data);
if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		$res = $user->checkPassword($fields['opass']);
		if(!$res) {
			Fox\Message::Error(Msg(Fox\Message::BUNDLE_ERRORS,6));
		}
		elseif($fields['npass'] != $fields['cpass']) {
			Fox\Message::Error(Msg(Fox\Message::BUNDLE_ERRORS,2));
		}
		elseif(empty($fields['npass'])) {
			Fox\Message::Error(Msg(Fox\Message::BUNDLE_ERRORS,7));
		}
		else {
			$fields['passw'] = md5($fields['npass']);
		}
	}
	if(!$screen->errors()) {
		try {
			$user = User::current();
			$user->setFields($fields);
			$user->update();
			Fox\Message::Success(Msg(Fox\Msg::BUNDLE,Fox\Msg::UPDATE_DONE));
			// @TODO send an email to the user to tell him that its password has been changed
			
		} catch(\Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
	}
	echo $xview->messages(true,true);
	exit;
}
$html = $screen->display();
?>
<div>
	<div class="page-header clearfix"><?php echo $header->render();?></div>
	<div class="contentArea">
		<div class="mbs ">
			<?php echo $html;?>
		</div>
	</div>
</div>